//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hustcad.plm.pdm.form.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.basic.log.TyplmLogger;
import com.hustcad.plm.basic.util.TySecurityLabelsUtil;
import com.hustcad.plm.pdm.common.model.vo.BusinessObjectVO;
import com.hustcad.plm.pdm.form.model.dto.FormDataDTO;
import com.hustcad.plm.pdm.form.model.dto.FormDataFileMarkDTO;
import com.hustcad.plm.pdm.form.model.dto.FormHistoryDTO;
import com.hustcad.plm.pdm.form.model.dto.FormSignatureDataQueryParamDTO;
import com.hustcad.plm.pdm.form.model.vo.FormDataCheckInVO;
import com.hustcad.plm.pdm.form.model.vo.FormDataVO;
import com.hustcad.plm.pdm.form.model.vo.FormSignatureDataVO;
import com.hustcad.plm.pdm.form.model.vo.TyplmFormDataVO;
import com.hustcad.plm.pdm.form.service.TyFormDataExtService;
import com.hustcad.plm.pdm.form.service.TyFormSignatureService;
import com.hustcad.plm.pdm.form.service.TyplmFormDataService;
import com.hustcad.plm.pdm.formusage.model.dto.FormFileDTO;
import com.jsyxdl.utils.FormTemplateAndDataUtils;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.audit.AuditLogDO;
import com.ty.basic.entity.form.FormDataDO;
import com.ty.basic.entity.form.FormDataMasterDO;
import com.ty.basic.response.ResponseResult;
import com.ty.security.util.TyCommonUtils;
import io.swagger.annotations.ApiOperation;
import java.math.BigInteger;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping({"/rest/v1/formData"})
public class FormDataController {
    private static final Logger log = LoggerFactory.getLogger(FormDataController.class);
    @Resource
    private TyplmFormDataService typlmFormDataService;
    @Resource
    private TyFormDataExtService tyFormDataExtService;
    @Resource
    private TyFormSignatureService tyFormSignatureService;
    @Resource
    private TyplmLogger typlmLogger;
    private static final String FORM_DATA_DTO = "formDataDTO";
    private static final String FORM_DATA_FILE_MARK_DTO_LIST = "formDataFileMarkDTOList";
    private static final String ADD_FILE_LIST = "addFileList";
    private static final String REMOVE_FILE_LIST = "removeFileList";

    public FormDataController() {
    }

    @PostMapping({"/createFormData"})
    @ApiOperation(
            value = "创建表单实例",
            notes = "创建表单实例",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormDataDO> createFormData(@RequestBody FormDataDTO formDataDTO) {
        try {
            FormDataDO formDataDO = this.tyFormDataExtService.createFormData(formDataDTO);
            this.typlmLogger.info(new CTyEntityBaseDO(formDataDO.getOid(), formDataDO.getOtype()), LogEventEnum.CRATE_OBJECT);
            return ResponseResult.success(formDataDO);
        } catch (Exception var3) {
            this.addAuditErrorLog(formDataDTO, var3);
            throw var3;
        }
    }

    private void addAuditErrorLog(@RequestBody FormDataDTO formDataDTO, Exception exception) {
        AuditLogDO auditLogDO = new AuditLogDO();
        auditLogDO.setObjoid(BigInteger.ZERO);
        auditLogDO.setObjotype("ty.inteplm.form.CTyFormData");
        auditLogDO.setContaineroid(formDataDTO.getContaineroid());
        auditLogDO.setContainerotype(formDataDTO.getContainerotype());
        auditLogDO.setTargettype(formDataDTO.getTypeDefName());
        auditLogDO.setTargetname(formDataDTO.getName());
        auditLogDO.setTargetnumber(formDataDTO.getObjectNumber());
        auditLogDO.setDomainoid(formDataDTO.getDomainoid());
        auditLogDO.setLifecyclestageoid(formDataDTO.getLifecyclestageoid());
        auditLogDO.setLifecyclestageotype(formDataDTO.getLifecyclestageotype());
        auditLogDO.setLifecyclestatekey(formDataDTO.getLifecyclestagekey());
        this.typlmLogger.errorInfo(LogEventEnum.CRATE_OBJECT, auditLogDO, exception);
    }

    @PostMapping({"/updateFormData"})
    @ApiOperation(
            value = "更新表单实例",
            notes = "更新表单实例",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormDataDO> updateFormData(@RequestBody FormDataDTO formDataDTO) {
        try {
            String oldSecurityLabels = TySecurityLabelsUtil.getSecurityLabels(formDataDTO.getOid(), "ty.inteplm.form.CTyFormData");
            FormDataDO formDataDO = this.tyFormDataExtService.updateFormData(formDataDTO);
            this.sendAuditLog(oldSecurityLabels, formDataDO);
            return ResponseResult.success(formDataDO);
        } catch (Exception var4) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(formDataDTO.getOid(), formDataDTO.getOtype()), LogEventEnum.UPDATE_OBJECT, var4);
            throw var4;
        }
    }

    @PostMapping({"/createFormDataWithFile"})
    @ApiOperation(
            value = "创建表单实例带文件",
            notes = "创建表单实例带文件",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormDataDO> createFormDataWithFile(List<MultipartFile> fileList, HttpServletRequest request) {
        FormDataDTO formDataDTO = (FormDataDTO)JSON.parseObject(request.getParameter("formDataDTO"), FormDataDTO.class);
        List<FormDataFileMarkDTO> formDataFileMarkDTOList = JSON.parseArray(request.getParameter("formDataFileMarkDTOList"), FormDataFileMarkDTO.class);
        List<BigInteger> addFileList = JSON.parseArray(request.getParameter("addFileList"), BigInteger.class);
        List<BigInteger> removeFileList = JSON.parseArray(request.getParameter("removeFileList"), BigInteger.class);
        FormFileDTO formFileDTO = new FormFileDTO();
        formFileDTO.setAddFileList(addFileList);
        formFileDTO.setRemoveFileList(removeFileList);

        try {
            FormDataDO formDataDO = this.tyFormDataExtService.createFormDataWithFile(formDataDTO, formDataFileMarkDTOList, fileList, formFileDTO);
            this.typlmLogger.info(new CTyEntityBaseDO(formDataDO.getOid(), formDataDO.getOtype()), LogEventEnum.CRATE_OBJECT);
            return ResponseResult.success(formDataDO);
        } catch (Exception var9) {
            this.addAuditErrorLog(formDataDTO, var9);
            throw var9;
        }
    }

    @PostMapping({"/updateFormDataWithFile"})
    @ApiOperation(
            value = "更新表单实例带文件",
            notes = "更新表单实例带文件",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormDataDO> updateFormDataWithFile(List<MultipartFile> fileList, HttpServletRequest request) {
        FormDataDTO formDataDTO = (FormDataDTO)JSON.parseObject(request.getParameter("formDataDTO"), FormDataDTO.class);
        List<FormDataFileMarkDTO> formDataFileMarkDTOList = JSON.parseArray(request.getParameter("formDataFileMarkDTOList"), FormDataFileMarkDTO.class);
        List<BigInteger> addFileList = JSON.parseArray(request.getParameter("addFileList"), BigInteger.class);
        List<BigInteger> removeFileList = JSON.parseArray(request.getParameter("removeFileList"), BigInteger.class);
        FormFileDTO formFileDTO = new FormFileDTO();
        formFileDTO.setAddFileList(addFileList);
        formFileDTO.setRemoveFileList(removeFileList);

        try {
            String oldSecurityLabels = TySecurityLabelsUtil.getSecurityLabels(formDataDTO.getOid(), "ty.inteplm.form.CTyFormData");
            FormDataDO formDataDO = this.tyFormDataExtService.updateFormDataWithFile(fileList, formDataDTO, formDataFileMarkDTOList, formFileDTO);
            this.sendAuditLog(oldSecurityLabels, formDataDO);
            return ResponseResult.success(formDataDO);
        } catch (Exception var10) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(formDataDTO.getOid(), formDataDTO.getOtype()), LogEventEnum.UPDATE_OBJECT, var10);
            throw var10;
        }
    }

    private void sendAuditLog(String oldSecurityLabels, FormDataDO formDataDO) {
        if (!StringUtils.equals(oldSecurityLabels, formDataDO.getSecuritylabels())) {
            this.typlmLogger.info(new CTyEntityBaseDO(formDataDO.getOid(), formDataDO.getOtype()), LogEventEnum.UPDATE_OBJECT, String.format("密级[%s -> %s]", oldSecurityLabels, formDataDO.getSecuritylabels()));
        } else {
            this.typlmLogger.info(new CTyEntityBaseDO(formDataDO.getOid(), formDataDO.getOtype()), LogEventEnum.UPDATE_OBJECT);
        }

    }

    @PostMapping({"/deleteFormData"})
    @ApiOperation(
            value = "删除表单实例",
            notes = "删除表单实例",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<String> deleteFormData(@RequestBody BigInteger oid) {
        try {
            this.typlmLogger.info(new CTyEntityBaseDO(oid, "ty.inteplm.form.CTyFormData"), LogEventEnum.DELETE_OBJECT);
            this.typlmFormDataService.deleteFormData(oid);
            return ResponseResult.success();
        } catch (Exception var3) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(oid, "ty.inteplm.form.CTyFormData"), LogEventEnum.DELETE_OBJECT, var3);
            throw var3;
        }
    }

    @PostMapping({"/getFormDataByRefObj"})
    @ApiOperation(
            value = "获取对象关联表单实例",
            notes = "获取对象关联表单实例",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormDataVO> getFormDataByRefObj(@RequestBody IdentifierEntity refIdentifier) {
        return ResponseResult.success(this.typlmFormDataService.getFormDataByRefObj(refIdentifier));
    }

    @PostMapping({"/getFormDataByOid"})
    @ApiOperation(
            value = "根据oid获取表单实例",
            notes = "根据oid获取表单实例",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<TyplmFormDataVO> getFormDataByOid(@RequestBody BigInteger oid) {
        return ResponseResult.success(this.tyFormDataExtService.getFormDataDetail(oid));
    }

    @PostMapping({"/queryFormDataMasterByName"})
    @ApiOperation(
            value = "根据name查询表单实例主对象",
            notes = "根据name查询表单实例主对象",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<FormDataMasterDO>> queryFormDataMasterByName(@RequestBody String name) {
        return ResponseResult.success(this.typlmFormDataService.queryFormDataMasterByName(name));
    }

    @PostMapping({"/setFormDataStatus"})
    @ApiOperation(
            value = "设置表单状态",
            notes = "设置表单状态",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<String> setFormDataStatus(@RequestBody FormDataDTO formDataDTO) {
        try {
            this.typlmFormDataService.setFormDataStatus(formDataDTO);
            this.typlmLogger.info(new CTyEntityBaseDO(formDataDTO.getOid(), "ty.inteplm.form.CTyFormData"), LogEventEnum.CHANGE_LC_STATE);
            return ResponseResult.success();
        } catch (Exception var3) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(formDataDTO.getOid(), "ty.inteplm.form.CTyFormData"), LogEventEnum.CHANGE_LC_STATE, var3);
            throw var3;
        }
    }

    @PostMapping({"/getFormSignature"})
    @ApiOperation(
            value = "查询指定表单的签名信息",
            notes = "查询指定表单的签名信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormSignatureDataVO> getFormSignature(@RequestBody FormSignatureDataQueryParamDTO formDataDTO) {
        return ResponseResult.success(this.tyFormSignatureService.getFormSignature(formDataDTO));
    }

    @PostMapping({"/getFormValueSignature"})
    @ApiOperation(
            value = "查询指定对象关联表单的签名信息",
            notes = "查询指定对象关联表单的签名信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormSignatureDataVO> getFormValueSignature(@RequestBody FormSignatureDataQueryParamDTO formDataDTO) {
        return ResponseResult.success(this.tyFormSignatureService.getFormValueSignature(formDataDTO));
    }

    @PostMapping({"/getFormDataHasContainerByOid"})
    @ApiOperation(
            value = "根据oid获取表单实例带容器数据",
            notes = "根据oid获取表单实例带容器数据",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<TyplmFormDataVO> getFormDataHasContainerByOid(@RequestBody BigInteger oid) {
        try {
            TyplmFormDataVO formDataVO = this.tyFormDataExtService.getFormDataDetail(oid);
            if (formDataVO != null) {
                this.typlmLogger.info(new CTyEntityBaseDO(formDataVO.getOid(), formDataVO.getOtype()), LogEventEnum.VIEW_OBJECT);
            }

            return ResponseResult.success(formDataVO);
        } catch (Exception var3) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(oid, "ty.inteplm.form.CTyFormData"), LogEventEnum.VIEW_OBJECT, var3);
            throw var3;
        }
    }

    @GetMapping({"/getFormDataHasContainerByOid"})
    @ApiOperation(
            value = "根据oid获取表单实例带容器数据",
            notes = "根据oid获取表单实例带容器数据",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<TyplmFormDataVO> getFormDataByOid(@RequestParam BigInteger oid, @RequestParam(required = false) String isQueryForCopy) {
        try {
            TyplmFormDataVO formDataVO;
            if (StringUtils.equalsIgnoreCase("Y", isQueryForCopy)) {
                formDataVO = this.tyFormDataExtService.getFormDataDetailForCopy(oid);
            } else {
                formDataVO = this.tyFormDataExtService.getFormDataDetail(oid);
            }

            if (formDataVO != null) {
                FormTemplateAndDataUtils.getFormDataHasContainerByOid(formDataVO);
                this.typlmLogger.info(new CTyEntityBaseDO(formDataVO.getOid(), formDataVO.getOtype()), LogEventEnum.VIEW_OBJECT);
            }

            return ResponseResult.success(formDataVO);
        } catch (Exception var4) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(oid, "ty.inteplm.form.CTyFormData"), LogEventEnum.VIEW_OBJECT, var4);
            throw var4;
        }
    }

    @GetMapping({"/getUserTaskGrantedFormDataByOid"})
    @ApiOperation(
            value = "根据oid获取授权表单实例",
            notes = "根据oid获取授权表单实例",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<TyplmFormDataVO> getUserTaskGrantedFormDataByOid(@RequestParam BigInteger formDataOid, @RequestParam String taskId) {
        try {
            TyplmFormDataVO formDataVO = this.tyFormDataExtService.getFormDataDetail(formDataOid, taskId);
            if (formDataVO != null) {
                this.typlmLogger.info(new CTyEntityBaseDO(formDataVO.getOid(), formDataVO.getOtype()), LogEventEnum.VIEW_OBJECT);
            }

            return ResponseResult.success(formDataVO);
        } catch (Exception var4) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(formDataOid, "ty.inteplm.form.CTyFormData"), LogEventEnum.VIEW_OBJECT, var4);
            throw var4;
        }
    }

    @PostMapping({"/formDataHistory"})
    @ApiOperation(
            value = "查询表单历史",
            notes = "查询表单历史",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<FormHistoryDTO>> getFormDataHistory(@RequestBody IdentifierEntity identifierEntity) {
        TyCommonUtils.requireNonNull(identifierEntity.getOid(), "oid");
        return ResponseResult.success(this.typlmFormDataService.getFormDataHistory(identifierEntity.getOid(), ""));
    }

    @PostMapping({"/saveAndCheckIn"})
    @ApiOperation(
            value = "表单实例保存数据并检入",
            notes = "表单实例保存数据并检入",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<BusinessObjectVO> saveAndCheckIn(List<MultipartFile> fileList, HttpServletRequest request) {
        FormDataCheckInVO formDataCheckInVO = (FormDataCheckInVO)JSON.parseObject(request.getParameter("formDataCheckInVO"), FormDataCheckInVO.class);
        List<FormDataFileMarkDTO> formDataFileMarkDTOList = JSON.parseArray(request.getParameter("formDataFileMarkDTOList"), FormDataFileMarkDTO.class);
        List<BigInteger> addFileList = JSON.parseArray(request.getParameter("addFileList"), BigInteger.class);
        List<BigInteger> removeFileList = JSON.parseArray(request.getParameter("removeFileList"), BigInteger.class);
        FormFileDTO formFileDTO = new FormFileDTO();
        formFileDTO.setAddFileList(addFileList);
        formFileDTO.setRemoveFileList(removeFileList);

        try {
            BusinessObjectVO businessObjectVO = this.tyFormDataExtService.saveAndCheckIn(formDataCheckInVO, fileList, formDataFileMarkDTOList, formFileDTO);
            this.typlmLogger.info(new CTyEntityBaseDO(businessObjectVO.getId(), StringUtils.split(formDataCheckInVO.getTypeDefName(), '|')[0]), LogEventEnum.UPDATE_CONTENT);
            this.typlmLogger.info(new CTyEntityBaseDO(businessObjectVO.getId(), StringUtils.split(formDataCheckInVO.getTypeDefName(), '|')[0]), LogEventEnum.CHECK_IN);
            return ResponseResult.success(businessObjectVO);
        } catch (Exception var9) {
            if (StringUtils.isNotBlank(formDataCheckInVO.getId())) {
                this.typlmLogger.errorInfo(new CTyEntityBaseDO(new BigInteger(formDataCheckInVO.getId()), StringUtils.split(formDataCheckInVO.getTypeDefName(), '|')[0]), LogEventEnum.UPDATE_CONTENT, var9);
            }

            throw var9;
        }
    }

    @PostMapping({"/saveFormContent"})
    @ApiOperation(
            value = "保存表单内容数据带文件",
            notes = "保存表单内容数据带文件",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormDataDO> saveFormContent(List<MultipartFile> fileList, HttpServletRequest request) {
        FormDataDTO formDataDTO = (FormDataDTO)JSON.parseObject(request.getParameter("formDataDTO"), FormDataDTO.class);
        List<FormDataFileMarkDTO> formDataFileMarkDTOList = JSON.parseArray(request.getParameter("formDataFileMarkDTOList"), FormDataFileMarkDTO.class);
        List<BigInteger> addFileList = JSON.parseArray(request.getParameter("addFileList"), BigInteger.class);
        List<BigInteger> removeFileList = JSON.parseArray(request.getParameter("removeFileList"), BigInteger.class);
        FormFileDTO formFileDTO = new FormFileDTO();
        formFileDTO.setAddFileList(addFileList);
        formFileDTO.setRemoveFileList(removeFileList);

        try {
            this.tyFormDataExtService.saveFormContent(formDataDTO, fileList, formDataFileMarkDTOList, formFileDTO);
            this.typlmLogger.info(new CTyEntityBaseDO(formDataDTO.getOid(), formDataDTO.getOtype()), LogEventEnum.UPDATE_CONTENT);
            return ResponseResult.success(true);
        } catch (Exception var9) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(formDataDTO.getOid(), formDataDTO.getOtype()), LogEventEnum.UPDATE_CONTENT, var9);
            throw var9;
        }
    }

    @GetMapping({"/getLatestFormVersion"})
    @ApiOperation(
            value = "获取表单最新版本",
            notes = "获取表单最新版本",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<TyplmFormDataVO> getLatestFormVersion(@RequestParam BigInteger masteredoid) {
        return ResponseResult.success(this.tyFormDataExtService.getLatestFormVersionDetail(masteredoid));
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @DeleteMapping({"/deleteLatestForm"})
    @ApiOperation(
            value = "删除表单最新版本",
            notes = "删除表单最新版本",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Object> deleteLatestForm(@RequestBody IdentifierEntity identifierEntity) {
        this.typlmFormDataService.deleteLatestForm(identifierEntity.getOid());
        return ResponseResult.success();
    }
}
